package com.ddstation.app.report.service;

import com.ddstation.app.report.model.ReportUploadModel;
import com.ddstation.crud.entity.DtInvsReportUploadEntity;
import com.ddstation.crud.mapper.DtInvsReportUploadMapper;
import com.ddstation.home_page.service.DtInvsMessageQueueService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service @Lazy
public class ReportUploadServiceImpl implements ReportUploadService {

	@Inject private DtInvsReportUploadMapper dtInvsReportUploadMapper;
	@Inject private DtInvsMessageQueueService dtInvsMessageQueueService;

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void saveReportInfo(ReportUploadModel reportInfo) {
		// TODO Auto-generated method stub
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			reportInfo.setCheckDate(format.parse(reportInfo.getCheckDateStr()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		DtInvsReportUploadEntity entity = new DtInvsReportUploadEntity();
		entity.setInstitutionId(reportInfo.getInstitutionId());
		entity.setCheckDate(reportInfo.getCheckDate());
		entity.setReportType(reportInfo.getReportType());
		entity.setQualifiedFlg(reportInfo.getQualifiedFlg());
		entity.setReportImgId(reportInfo.getReportImgId());
		entity.setComment(reportInfo.getComment());
		entity.setDoctorId(reportInfo.getDoctorId());
		dtInvsReportUploadMapper.insert(entity);

		//新增消息
		dtInvsMessageQueueService.insertMessageQueue(reportInfo.getInstitutionId(),301,1,1,entity.getId().toString(),1);
	}

	@Override
	public DtInvsReportUploadEntity queryReportInfo(DtInvsReportUploadEntity entity) {
		return dtInvsReportUploadMapper.select(entity);
	}

	@Override
	public List<DtInvsReportUploadEntity> selectByHistory(String institutionId,Integer reportType, int pageNo, int pageSize) {
		int startNo = (pageNo - 1) * pageSize;
		return dtInvsReportUploadMapper.selectByHistory(institutionId,reportType,startNo,pageSize);
	}
}
